<?php
class RegionsController extends Controller {

	public function actionIndex() {
        $title = Yii::t('app', 'Список регионов');
        $this->pageTitle = Yii::t('app', 'Список регионов');
        $r_id = intval(CHttpRequest::getParam('rid'));
        if (0 < $r_id && isset(Yii::app()->basestatic->all_regions[$r_id])) {
            $regions = array(0 => Yii::app()->basestatic->all_regions[$r_id]);
        } else {
            $regions = Yii::app()->basestatic->all_regions;
        }
        
		$this->render('index', array(
            'title' => $title,
            'regions' => $regions,
        ));
	}
    
    public function actionRegions() {
    /*
        $regions_tmp = Yii::app()->basestatic->all_regions;
        $regions = array();
        
        foreach($regions_tmp as $region) {
            $data_tmp = array(
                'id' => $region['id'],
                'name' => GxHtml::encode($region['name']),
            );    
            if (0 < count($region['cities'])) {
                $data_tmp['url'] = Yii::app()->createUrl('regions/index', array('rid' => $region['id']));
                $data_tmp['has_subregions'] = true;
            } else {
                $data_tmp['url'] = Yii::app()->createUrl('regions/setdata', array('rid' => $region['id']));
                $data_tmp['has_subregions'] = false;
            }     
            $regions[] = $data_tmp;
        } 
*/        
        $this->ajax_responce($this->regionsData());
    }
    
    public function actionCities() {
        $r_id = intval(CHttpRequest::getParam('rid'));
        if (0 >= $r_id) $this->ajax_responce(array('error' => 'bad region id'));
        if (!isset(Yii::app()->basestatic->all_regions[$r_id])) {
            $this->ajax_responce(array('error' => 'region data not found'));
        }
        $cities_tmp = Yii::app()->basestatic->all_regions[$r_id]['cities'];
        
        $cities[] = array(
            'id' => 0,
            'url' => Yii::app()->createUrl('regions/setdata', array('rid' => $r_id)),
            'name' => Yii::t('app', 'Все города'),
            'has_subregions' => false                
        );
        foreach($cities_tmp as $city_tmp) {
            $cities[] = array(
                'id' => $city_tmp['id'],
                'url' => Yii::app()->createUrl('regions/setdata', array('rid' => $city_tmp['region_id'], 'cid' => $city_tmp['id'])),
                'name' => GxHtml::encode($city_tmp['name']),
                'has_subregions' => false
            );    
        }
        $this->ajax_responce($cities);
    }
    
    public function actionSetdata() {
        $r_id = intval(CHttpRequest::getParam('rid'));
        $c_id = intval(CHttpRequest::getParam('cid'));
        Yii::app()->session['region_id'] = $r_id;
        Yii::app()->session['city_id'] = $c_id;
		
		//!!!!!!!!!!!!!!!
		//adding time() function ONLY for yii cookie class, because it required, but in usual cookie setting the time() functiont not needed!!!!!!
        $lifetime = time() + 2592000; //30 days 
        $this->setCookie('region_id', $r_id, $lifetime);
        $this->setCookie('city_id', $c_id, $lifetime);
		// Debug::dd(array(
			// 'region_id' => $this->getCookie('region_id'),
			// 'city_id' => $this->getCookie('city_id'),
			// $_COOKIE
		// )
		// );
		
        $this->redirect(array('home/index'));
    }
    
}

?>